<?php
class UserController extends Zend_Controller_Action
{

    public function init () {
        $init = new Model_Init($this);
    }

    public function registrationAction () {
        $form = new Form_Registration();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                
                $user = new Model_User();
                $user->fill($form->getValues());
                $user->created = date('Y-m-d H:i:s');
                
                $mail_sender = new Model_PasswordMail();
                $password = $mail_sender->generatePassword();
                $user->password = md5($password);
                $user->save();
                
                //add login, name, surname to SearchLucence
                $user_item = new Model_User(
                $user->getLastInsertedId());
                
                $model = new Model_Search('users');
                $model->addToIndex($user_item->id, $user_item->name, 
                $user_item->surname, $user_item->login);
                
                //get email from $_post
                $email = $this->_getParam('email');
                $mail_sender->sendMailNewPass($email, $password, $this);
                
                $this->_helper->redirector('thankreg', 'user');
            } else {
                $this->view->errors = $form->getMessages();
            }
        }
        $this->view->regform = $form;
    }

    //show template with thanks for registration
    public function thankregAction () {}

    //view user info
    public function viewAction () {
        //if user is not log in, redirect to log in, and collect in registry value with that user
        if (! Zend_Auth::getInstance()->hasIdentity()) {
            //TODO collect only user name or add that to _init method
            $localSession = new Zend_Session_Namespace(
            'not_reg_view_url');
            //set default language
            $localSession->user = $this->_getParam('login');
            $this->_helper->redirector('index', 'index');
        }
        $user_name = $this->_getParam('login');
        //search user by login
        //TODO create cache
        $user_db = new Model_DbTable_Users();
        $user_model = $user_db->fetchRow(
        $user_db->select()
            ->where('login = ?', $user_name));
        //check if email exists
        if ($user_model) {
            $user = new Model_User($user_model->id);
            $this->view->user_info = $user;
        } else {
            //TODO what to do, if there isn't any user
            echo $this->view->translate("There is no user");
        }
    }

    public function logoutAction () { 
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_helper->redirector('index', 'index');
    }

    public function forgetpassAction () {
        $form = new Form_ForgetPass();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                $mail_sender = new Model_PasswordMail();
                $new_pass = $mail_sender->generatePassword();
                $email = $this->_getParam('email');
                $user = new Model_DbTable_Users();
                //search user by email in the DB
                $user_model = $user->fetchRow(
                $user->select()
                    ->where('email = ?', $email));
                //check if email exists
                if ($user_model) {
                    $user_id = $user_model->id;
                    $user = new Model_User($user_id);
                    $user->password = md5($new_pass);
                    $user->save();
                    //
                    $mail_sender->sendMailNewPass($email, 
                    $new_pass, $this);
                    $this->view->message = $this->view->translate(
                    'Thank fill forgot pass');
                } else {
                    $this->view->message = $this->view->translate(
                    'Can not find email');
                }
            } else {
                $this->view->errors = $form->getMessages();
            }
        }
        $this->view->forgetform = $form;
    }

    //use meethod for writing points [X,Y] of avatar coordinates to DB from jQuery $.post
    public function writepointAction () {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //TODO check if user is authorize and is that user!!!
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            if ($this->getRequest()->isPost()) {
                //get ID user from session
                $id = Zend_Auth::getInstance()->getIdentity()->id;
                $user = new Model_User($id);
                $user->ava_x = $this->_getParam('x_coordinate');
                $user->ava_y = $this->_getParam('y_coordinate');
                $user->save();
            }
        }
    }

    //TODO refactor it with function writepointAction
    public function writesexAction () {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //TODO check if user is authorize and is that user!!!
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            if ($this->getRequest()->isPost()) {
                //get ID user from session
                $id = Zend_Auth::getInstance()->getIdentity()->id;
                $user = new Model_User($id);
                $user->sex_x = $this->_getParam('x_coordinate');
                $user->sex_y = $this->_getParam('y_coordinate');
                $user->save();
            }
        }
    }

    public function writestatusAction () {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //TODO check if user is authorize and is that user!!!
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            if ($this->getRequest()->isPost()) {
                //get ID user from session
                $id = Zend_Auth::getInstance()->getIdentity()->id;
                $user = new Model_User($id);
                $user->status_x = $this->_getParam('x_coordinate');
                $user->status_y = $this->_getParam('y_coordinate');
                $user->save();
            }
        }
    }
    
    public function writealbumAction () { 
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //TODO check if user is authorize and is that user!!!
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            if ($this->getRequest()->isPost()) {
                //get ID user from session
                $id = Zend_Auth::getInstance()->getIdentity()->id;
                $user = new Model_User($id);
                $user->album_x = $this->_getParam('x_coordinate');
                $user->album_y = $this->_getParam('y_coordinate');
                $user->save();
            }
        }
    }
}
